package StudyCode.summary.csv;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CSVListArrayString {
	public static void main(String[] argv) throws Exception {
		String str = "www.tah1986.com";
		System.out.println(Arrays.toString(commaDelimitedListToStringArray(str)));
	}


	private static String[] commaDelimitedListToStringArray(final String str) {

		return delimitedListToStringArray(str, ",");
	}

	private static String[] delimitedListToStringArray(final String str, final String delimiter) {

		return delimitedListToStringArray(str, delimiter, null);
	}


	private static String[] delimitedListToStringArray(final String str, final String delimiter, final String charsToDelete) {

		if (str == null)
			return new String[0];
		if (delimiter == null)
			return new String[]{str};
		final List<String> result = new ArrayList<String>();
		if ("".equals(delimiter)) {
			for (int i = 0; i < str.length(); i++) {
				result.add(deleteAny(str.substring(i, i + 1), charsToDelete));
			}
		} else {
			int pos = 0;
			int delPos;
			while ((delPos = str.indexOf(delimiter, pos)) != -1) {
				result.add(deleteAny(str.substring(pos, delPos),
						charsToDelete));
				pos = delPos + delimiter.length();
			}
			if ((str.length() > 0) && (pos <= str.length())) {
				result.add(deleteAny(str.substring(pos), charsToDelete));
			}
		}
		return toStringArray(result);
	}

	private static String deleteAny(final String inString, final String charsToDelete) {

		if (!hasLength(inString) || !hasLength(charsToDelete))
			return inString;
		final StringBuilder sb = new StringBuilder();
		for (int i = 0; i < inString.length(); i++) {
			final char c = inString.charAt(i);
			if (charsToDelete.indexOf(c) == -1) {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	private static String[] toStringArray(final Collection<String> collection) {

		if (collection == null)
			return null;
		return collection.toArray(new String[collection.size()]);
	}

	private static boolean hasLength(final CharSequence str) {

		return ((str != null) && (str.length() > 0));
	}


	private static boolean hasLength(final String str) {

		return hasLength((CharSequence) str);
	}
}
